home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / idle / OutputWindow.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.4 KB  |  91 lines

  1. from Tkinter import *
  2. from EditorWindow import EditorWindow
  3. import re
  4. import tkMessageBox
  5.  
  6. class OutputWindow(EditorWindow):
  7.  
  8.     """An editor window that can serve as an output file.
  9.  
  10.     Also the future base class for the Python shell window.
  11.     This class has no input facilities.
  12.     """
  13.  
  14.     def __init__(self, *args):
  15.         apply(EditorWindow.__init__, (self,) + args)
  16.         self.text.bind("<<goto-file-line>>", self.goto_file_line)
  17.  
  18.     # Customize EditorWindow
  19.  
  20.     def ispythonsource(self, filename):
  21.         # No colorization needed
  22.         return 0
  23.  
  24.     def short_title(self):
  25.         return "Output"
  26.  
  27.     def maybesave(self):
  28.         # Override base class method -- don't ask any questions
  29.         if self.get_saved():
  30.             return "yes"
  31.         else:
  32.             return "no"
  33.  
  34.     # Act as output file
  35.  
  36.     def write(self, s, tags=(), mark="insert"):
  37.         self.text.insert(mark, str(s), tags)
  38.         self.text.see(mark)
  39.         self.text.update()
  40.  
  41.     def writelines(self, l):
  42.         map(self.write, l)
  43.  
  44.     # Our own right-button menu
  45.  
  46.     rmenu_specs = [
  47.         ("Go to file/line", "<<goto-file-line>>"),
  48.     ]
  49.  
  50.     file_line_pats = [
  51.         r'file "([^"]*)", line (\d+)',
  52.         r'([^\s]+)\((\d+)\)',
  53.         r'([^\s]+):\s*(\d+):',
  54.     ]
  55.  
  56.     file_line_progs = None
  57.  
  58.     def goto_file_line(self, event=None):
  59.         if self.file_line_progs is None:
  60.             l = []
  61.             for pat in self.file_line_pats:
  62.                 l.append(re.compile(pat, re.IGNORECASE))
  63.             self.file_line_progs = l
  64.         # x, y = self.event.x, self.event.y
  65.         # self.text.mark_set("insert", "@%d,%d" % (x, y))
  66.         line = self.text.get("insert linestart", "insert lineend")
  67.         for prog in self.file_line_progs:
  68.             m = prog.search(line)
  69.             if m:
  70.                 break
  71.         else:
  72.             tkMessageBox.showerror("No special line",
  73.                 "The line you point at doesn't look like "
  74.                 "a file name followed by a line number.",
  75.                 master=self.text)
  76.             return
  77.         filename, lineno = m.group(1, 2)
  78.         try:
  79.             f = open(filename, "r")
  80.             f.close()
  81.         except IOError, msg:
  82.             self.text.bell()
  83.             return
  84.         edit = self.flist.open(filename)
  85.         try:
  86.             lineno = int(lineno)
  87.         except ValueError, msg:
  88.             self.text.bell()
  89.             return
  90.         edit.gotoline(lineno)
  91.